/*
 * Created by JFormDesigner on Fri Jun 16 19:49:18 CST 2023
 */

package fm.ex_warehouse;

import dao.Dao_ex_ware;
import dao.Dao_warehousing;
import model.Customer;
import model.Goods;
import model.Ware;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class LookEx_ware extends JInternalFrame {
    String [] columns = {"商品编号", "商品名称", "类别", "单价", "数量","码洋","折扣","实洋"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            Goods goods = (Goods) list.get(i);

            results[i][0] = goods.getGoodsID();
            results[i][1] = goods.getGoodsName();
            results[i][2] = goods.getType();
            results[i][3] = goods.getPrice();
            results[i][4] = goods.getNumber();
            results[i][5] = goods.getTotal();
            results[i][6] = goods.getDiscount();
            results[i][7] = goods.getReal();
        }


        return results;
    }
    public LookEx_ware() {
        initComponents();
        iniData();
        iniData_1();
    }

    public void iniData()
    {
        Vector<String> its = Dao_ex_ware.getWareID();
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
        comboBox.setModel(model);

        String wareID = (String) comboBox.getSelectedItem();

        Object [][] results = getFS(Dao_ex_ware.select_goods(wareID));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        table1.setModel(model_1);

        int setRow = table1.getRowCount();
        int goods_counts = 0;
        double goods_reals = 0.0;
        double goods_totals = 0.0;

        for(int i = 0; i < setRow; i++)
        {
            int counts = Integer.parseInt(table1.getValueAt(i,4).toString().trim());
            double totals = Double.parseDouble(table1.getValueAt(i, 5).toString().trim());
            double reals = Double.parseDouble(table1.getValueAt(i, 7).toString().trim());

            goods_counts += counts;
            goods_reals += reals;
            goods_totals += totals;

        }

        double goods_discounts = goods_totals - goods_reals;
        numberTextField.setText(String.valueOf(goods_counts));
        discountsTextField.setText(String.valueOf(goods_discounts));
        realsTextField.setText(String.valueOf(goods_reals));
        totalsTextField.setText(String.valueOf(goods_totals));
    }

    public void iniData_1()
    {
        String wareID = (String) comboBox.getSelectedItem();

        Object [][] results = getFS(Dao_ex_ware.select_goods(wareID));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        table1.setModel(model_1);

        int setRow = table1.getRowCount();
        int goods_counts = 0;
        double goods_reals = 0.0;
        double goods_totals = 0.0;

        for(int i = 0; i < setRow; i++)
        {
            int counts = Integer.parseInt(table1.getValueAt(i,4).toString().trim());
            double totals = Double.parseDouble(table1.getValueAt(i, 5).toString().trim());
            double reals = Double.parseDouble(table1.getValueAt(i, 7).toString().trim());

            goods_counts += counts;
            goods_reals += reals;
            goods_totals += totals;

        }

        double goods_discounts = goods_totals - goods_reals;
        numberTextField.setText(String.valueOf(goods_counts));
        discountsTextField.setText(String.valueOf(goods_discounts));
        realsTextField.setText(String.valueOf(goods_reals));
        totalsTextField.setText(String.valueOf(goods_totals));

        Customer list = Dao_ex_ware.select_All(wareID);

        outIDTextField.setText(list.getO_no());
        clientIDTextField.setText(list.getCusID());
        clientTextField.setText(list.getCusName());
        phoneTextField.setText(list.getCusPhone());
        addressTextField.setText(list.getCusAddress());
        timeTextField.setText(list.getTime());
    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void comboBox(ActionEvent e) {
        String wareID = (String) comboBox.getSelectedItem();

        Object [][] results = getFS(Dao_ex_ware.select_goods(wareID));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        table1.setModel(model_1);

        int setRow = table1.getRowCount();
        int goods_counts = 0;
        double goods_reals = 0.0;
        double goods_totals = 0.0;

        for(int i = 0; i < setRow; i++)
        {
            int counts = Integer.parseInt(table1.getValueAt(i,4).toString().trim());
            double totals = Double.parseDouble(table1.getValueAt(i, 5).toString().trim());
            double reals = Double.parseDouble(table1.getValueAt(i, 7).toString().trim());

            goods_counts += counts;
            goods_reals += reals;
            goods_totals += totals;

        }

        double goods_discounts = goods_totals - goods_reals;
        numberTextField.setText(String.valueOf(goods_counts));
        discountsTextField.setText(String.valueOf(goods_discounts));
        realsTextField.setText(String.valueOf(goods_reals));
        totalsTextField.setText(String.valueOf(goods_totals));

        Customer list = Dao_ex_ware.select_All(wareID);

        outIDTextField.setText(list.getO_no());
        clientIDTextField.setText(list.getCusID());
        clientTextField.setText(list.getCusName());
        phoneTextField.setText(list.getCusPhone());
        addressTextField.setText(list.getCusAddress());
        timeTextField.setText(list.getTime());
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        comboBox = new JComboBox();
        panel2 = new JPanel();
        cancelButton = new JButton();
        panel3 = new JPanel();
        panel4 = new JPanel();
        label1 = new JLabel();
        outIDTextField = new JTextField();
        label2 = new JLabel();
        clientIDTextField = new JTextField();
        label3 = new JLabel();
        clientTextField = new JTextField();
        label4 = new JLabel();
        addressTextField = new JTextField();
        label5 = new JLabel();
        phoneTextField = new JTextField();
        label6 = new JLabel();
        numberTextField = new JTextField();
        label7 = new JLabel();
        totalsTextField = new JTextField();
        label8 = new JLabel();
        realsTextField = new JTextField();
        label9 = new JLabel();
        discountsTextField = new JTextField();
        label10 = new JLabel();
        timeTextField = new JTextField();
        panel5 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("sunny\u51fa\u5e93\u6e05\u5355");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(0, 60));
            panel1.setBorder(new TitledBorder("\u8bf7\u9009\u62e9\u51fa\u5e93\u5355\u7f16\u53f7"));
            panel1.setOpaque(false);
            panel1.setLayout(null);

            //---- comboBox ----
            comboBox.addActionListener(e -> comboBox(e));
            panel1.add(comboBox);
            comboBox.setBounds(245, 20, 140, 29);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 643, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(0, 40));
            panel2.setLayout(null);

            //---- cancelButton ----
            cancelButton.setText("\u8fd4\u56de");
            cancelButton.addActionListener(e -> cancel(e));
            panel2.add(cancelButton);
            cancelButton.setBounds(277, 6, 75, cancelButton.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 439, 643, panel2.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setLayout(new BorderLayout());

            //======== panel4 ========
            {
                panel4.setPreferredSize(new Dimension(0, 180));
                panel4.setBorder(new TitledBorder("\u51fa\u5e93\u4fe1\u606f"));
                panel4.setLayout(null);

                //---- label1 ----
                label1.setText("\u51fa\u5e93\u5355\u7f16\u53f7\uff1a");
                panel4.add(label1);
                label1.setBounds(35, 30, label1.getPreferredSize().width, 20);

                //---- outIDTextField ----
                outIDTextField.setEditable(false);
                panel4.add(outIDTextField);
                outIDTextField.setBounds(105, 25, 105, outIDTextField.getPreferredSize().height);

                //---- label2 ----
                label2.setText("\u5ba2\u6237\u7f16\u53f7\uff1a");
                panel4.add(label2);
                label2.setBounds(249, 30, label2.getPreferredSize().width, 21);

                //---- clientIDTextField ----
                clientIDTextField.setEditable(false);
                panel4.add(clientIDTextField);
                clientIDTextField.setBounds(309, 25, 105, clientIDTextField.getPreferredSize().height);

                //---- label3 ----
                label3.setText("\u5ba2\u6237\u59d3\u540d\uff1a");
                panel4.add(label3);
                label3.setBounds(451, 30, label3.getPreferredSize().width, 20);

                //---- clientTextField ----
                clientTextField.setEditable(false);
                panel4.add(clientTextField);
                clientTextField.setBounds(512, 26, 92, clientTextField.getPreferredSize().height);

                //---- label4 ----
                label4.setText("\u5730\u5740\uff1a");
                panel4.add(label4);
                label4.setBounds(270, 83, label4.getPreferredSize().width, 22);

                //---- addressTextField ----
                addressTextField.setEditable(false);
                panel4.add(addressTextField);
                addressTextField.setBounds(309, 80, 104, addressTextField.getPreferredSize().height);

                //---- label5 ----
                label5.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
                panel4.add(label5);
                label5.setBounds(44, 81, label5.getPreferredSize().width, 21);

                //---- phoneTextField ----
                phoneTextField.setEditable(false);
                panel4.add(phoneTextField);
                phoneTextField.setBounds(105, 78, 104, phoneTextField.getPreferredSize().height);

                //---- label6 ----
                label6.setText("\u603b\u6570\u91cf\uff1a");
                panel4.add(label6);
                label6.setBounds(519, 132, label6.getPreferredSize().width, 18);

                //---- numberTextField ----
                numberTextField.setEditable(false);
                panel4.add(numberTextField);
                numberTextField.setBounds(574, 127, 52, numberTextField.getPreferredSize().height);

                //---- label7 ----
                label7.setText("\u603b\u7801\u6d0b\uff1a");
                panel4.add(label7);
                label7.setBounds(28, 130, label7.getPreferredSize().width, 21);

                //---- totalsTextField ----
                totalsTextField.setEditable(false);
                panel4.add(totalsTextField);
                totalsTextField.setBounds(79, 127, 86, totalsTextField.getPreferredSize().height);

                //---- label8 ----
                label8.setText("\u603b\u5b9e\u6d0b\uff1a");
                panel4.add(label8);
                label8.setBounds(190, 132, label8.getPreferredSize().width, 20);

                //---- realsTextField ----
                realsTextField.setEditable(false);
                panel4.add(realsTextField);
                realsTextField.setBounds(243, 128, 89, realsTextField.getPreferredSize().height);

                //---- label9 ----
                label9.setText("\u4f18\u60e0\u91d1\u989d\uff1a");
                panel4.add(label9);
                label9.setBounds(364, 133, label9.getPreferredSize().width, 19);

                //---- discountsTextField ----
                discountsTextField.setEditable(false);
                panel4.add(discountsTextField);
                discountsTextField.setBounds(425, 127, 67, discountsTextField.getPreferredSize().height);

                //---- label10 ----
                label10.setText("\u51fa\u5e93\u65e5\u671f\uff1a");
                panel4.add(label10);
                label10.setBounds(451, 82, label10.getPreferredSize().width, 20);

                //---- timeTextField ----
                timeTextField.setEditable(false);
                panel4.add(timeTextField);
                timeTextField.setBounds(513, 78, 92, timeTextField.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel4.getComponentCount(); i++) {
                        Rectangle bounds = panel4.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel4.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel4.setMinimumSize(preferredSize);
                    panel4.setPreferredSize(preferredSize);
                }
            }
            panel3.add(panel4, BorderLayout.NORTH);

            //======== panel5 ========
            {
                panel5.setBorder(new TitledBorder("\u51fa\u5e93\u5546\u54c1\u660e\u7ec6"));
                panel5.setLayout(null);

                //======== scrollPane1 ========
                {
                    scrollPane1.setViewportView(table1);
                }
                panel5.add(scrollPane1);
                scrollPane1.setBounds(10, 20, 625, 170);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel5.getComponentCount(); i++) {
                        Rectangle bounds = panel5.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel5.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel5.setMinimumSize(preferredSize);
                    panel5.setPreferredSize(preferredSize);
                }
            }
            panel3.add(panel5, BorderLayout.CENTER);
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 60, 643, 379);

        contentPane.setPreferredSize(new Dimension(655, 510));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JComboBox comboBox;
    private JPanel panel2;
    private JButton cancelButton;
    private JPanel panel3;
    private JPanel panel4;
    private JLabel label1;
    private JTextField outIDTextField;
    private JLabel label2;
    private JTextField clientIDTextField;
    private JLabel label3;
    private JTextField clientTextField;
    private JLabel label4;
    private JTextField addressTextField;
    private JLabel label5;
    private JTextField phoneTextField;
    private JLabel label6;
    private JTextField numberTextField;
    private JLabel label7;
    private JTextField totalsTextField;
    private JLabel label8;
    private JTextField realsTextField;
    private JLabel label9;
    private JTextField discountsTextField;
    private JLabel label10;
    private JTextField timeTextField;
    private JPanel panel5;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
